package com.ljb.calendar.month;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.ljb.calendar.DateViewAdapter;
import com.ljb.calendar.OnDateViewClickListener;
import com.ljb.calendar.OnPageSelectedListener;

import org.joda.time.LocalDate;

/**
 * Created by ljb on 2018/3/21.
 */

public class MonthCalendarView extends ViewPager {
    private static final String TAG = "MonthCalendarView";

    private static final int HALF_INTEGER_MAX_VASLUE = Integer.MAX_VALUE / 2;
    private SparseArray<MonthView> mMonthViews = new SparseArray<>();
    private LocalDate mTodayDate;
    private DateViewAdapter mDateViewAdapter;
    private OnDateViewClickListener mOnDateViewClickListener;
    private OnPageSelectedListener mOnPageSelectedListener;


    public MonthCalendarView(Context context) {
        this(context, null);
    }

    public MonthCalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(true);
        initStartDate();
        setAdapter(mPagerAdapter);
        addOnPageChangeListener(mOnPageChangeListener);
        setCurrentItem(HALF_INTEGER_MAX_VASLUE, false);
    }

    private void initStartDate() {
        mTodayDate = LocalDate.now();
    }

    public void setDateViewAdapter(DateViewAdapter dateViewAdapter) {
        mDateViewAdapter = dateViewAdapter;
    }

    public void setOnDateViewClickListener(OnDateViewClickListener onDateViewClickListener) {
        mOnDateViewClickListener = onDateViewClickListener;
    }

    public void setOnPageSelected(OnPageSelectedListener onPageSelectedListener) {
        mOnPageSelectedListener = onPageSelectedListener;
    }

    private PagerAdapter mPagerAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            mMonthViews.remove(position);
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (mMonthViews.get(position) == null) {
                MonthView monthView = new MonthView(container.getContext());
                monthView.setDateViewAdapter(mDateViewAdapter);
                monthView.setOnDateViewClickListener(mOnDateViewClickListener);

                LocalDate plusMonths = mTodayDate.plusMonths(position - HALF_INTEGER_MAX_VASLUE);
                int year = plusMonths.getYear();
                int month = plusMonths.getMonthOfYear();
                monthView.setData(mTodayDate, year, month, position);
                mMonthViews.put(position, monthView);
            }
            container.addView(mMonthViews.get(position));
            return mMonthViews.get(position);
        }
    };

    private OnPageChangeListener mOnPageChangeListener = new SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(final int position) {
            MonthView monthView = mMonthViews.get(position);
            if (monthView != null) {
                if (mOnPageSelectedListener != null)
                    mOnPageSelectedListener.onPageSelected(monthView, position);
            } else {
                MonthCalendarView.this.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onPageSelected(position);
                    }
                }, 50);
            }
        }
    };
}
